package com.romotc.qqrobot.webqq.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/*
 * QQMd5ͨ���������֤��õ���½ʹ�õ�����
 * 
 */
public class QQPswdUtil {

	public static String mdP(String p, String code){  
		Object t = null ;
        try {  
            ScriptEngineManager m = new ScriptEngineManager();  
            ScriptEngine se = m.getEngineByName("javascript");  
            se.eval(new FileReader(new File("web" + File.separator + "comm.js")));  
            t = se.eval("md5(md5_3(\""+p+"\")+\""+code.toUpperCase()+"\");");  
            return t.toString();  
        }catch (Exception e) {  
            e.printStackTrace();  
        }   
        return t.toString();  
    }  
	
	public static void main(String[] args){
//		System.out.println(mdP("210132385qwer","!4SN"));
		System.out.println("\u767B\u5F55\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5");
	}

	/**
	 * 注册QQ号时的密码加密
	 * @param pswd
	 * @return
	 */
	public static String rsa(String pswd) {
        try {
            ScriptEngineManager m = new ScriptEngineManager();  
            ScriptEngine se = m.getEngineByName("javascript");
//            se.eval(new FileReader(new File("web" + File.separator + "simple_Format.js")));
			se.eval(new FileReader(new File("web" + File.separator + "rsa_format1.js")));
			se.eval(new FileReader(new File("web" + File.separator + "rsa1.js")));
//			se.eval(new FileReader(new File("web" + File.separator + "index.js")));
			Object encryptPswd = se.eval("rsaEncrypt(\"" + pswd + "\")");
			System.out.println(encryptPswd);
			return encryptPswd.toString();
//			Object o = se.eval("parseBigInt(\"C4D23C2DB0ECC904FE0CD0CBBCDC988C039D79E1BDA8ED4BFD4D43754EC9693460D15271AB43A59AD6D0F0EEE95424F70920F2C4A08DFDF03661300047CA3A6212E48204C1BE71A846E08DD2D9F1CBDDFF40CA00C10C62B1DD42486C70A09C454293BCA9ED4E7D6657E3F62076A14304943252A88EFA416770E0FBA270A141E7\", 16)");
//System.out.println(o);
//	        var b = new RSAKey;
//	        b.setPublic("C4D23C2DB0ECC904FE0CD0CBBCDC988C039D79E1BDA8ED4BFD4D43754EC9693460D15271AB43A59AD6D0F0EEE95424F70920F2C4A08DFDF03661300047CA3A6212E48204C1BE71A846E08DD2D9F1CBDDFF40CA00C10C62B1DD42486C70A09C454293BCA9ED4E7D6657E3F62076A14304943252A88EFA416770E0FBA270A141E7", "10001");
//	        return b.encrypt(a)
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ScriptException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}

}
